package com.yukefms.rule.formula.basis;

import com.yukefms.engine.running.MatchedStatus;
import com.yukefms.engine.running.RunningContext;
import com.yukefms.jointpoint.BodyPointCloud;
import com.yukefms.jointpoint.GeometryRelations;
import com.yukefms.jointpoint.Kinect25JointType;
import com.yukefms.jointpoint.interfaces.Position;
import com.yukefms.rule.formula.interfaces.AbstractStaticBAFormula;
import com.yukefms.rule.formula.interfaces.DynamicBAFormula;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @Description the formula is to specify body part moving up, for example hands, foot, ect, which is dynamic action and should be measured by current
 * point cloud and previous one. Currently, the previous one is set by at most the 10th old one, which is called
 * LAST_NUM configured in {@code EngineConfig}.
 * @Author Ping
 * @Date 2021/7/16 0:33
 **/
public class MovingDownDynamicBAFormula extends AbstractStaticBAFormula implements DynamicBAFormula {
    public static Logger logger = LogManager.getLogger(MovingDownDynamicBAFormula.class);

    private boolean isSlow ;
    private String  jointPoint ; // "0"


    public MovingDownDynamicBAFormula() {
        super(Kinect25JointType.JointType_SpineMid.toString()) ;
        // core body point is the default.
        this.jointPoint = Kinect25JointType.JointType_SpineMid.toString() ;
        this.isSlow = false ;
    }

    public MovingDownDynamicBAFormula(String jointPoint) {
        super(jointPoint) ;
        this.jointPoint = jointPoint ;
        this.isSlow = false ;
    }
    public MovingDownDynamicBAFormula(String jointPoint, boolean isSlow) {
        super(jointPoint) ;
        this.jointPoint = jointPoint ;
        this.isSlow = isSlow ;
    }

    @Override
    public MatchedStatus matched(BodyPointCloud bodyPointCloud, RunningContext context) {
        /*
        Comparing current pointcloud with LAST ONE on JOINT_TYPE_SpineBase.
        If current one is under below of last one, it was right.
         */

      /*  if (!context.hasCachedLastPosition()) {
            logger.error("NOTE: The cached pointCloud structure is empty now.") ;
            return MatchedStatus.FALSE ;
        }*/
        Position lastPosition = context.getLastPosition(jointPoint) ;
        Position newPosition = bodyPointCloud.getPosition(jointPoint) ;
/*        System.out.println("new: " + newPosition.getZ() + ", last: " + lastPosition.getZ()) ;*/
        // moving slowly.
        if (
          /*     GeometryRelations.isHorizonalBelow(bodyPointCloud.getPosition(jointPoint),
                lastPosition)*/
                GeometryRelations.slowlyIncreasedAtZ(lastPosition,bodyPointCloud.getPosition(jointPoint))
        ) {
            // if this formula has successor , then the matching result is decided by its successor.
            if (this.successor != null) {
                return this.successor.matched(bodyPointCloud, context);
            } else {
                return MatchedStatus.TRUE;
            }
        }
        return MatchedStatus.FALSE;
    }
}
